package cn.iocoder.yudao.module.other.controller.admin.weibo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.other.controller.admin.weibo.vo.*;
import cn.iocoder.yudao.module.other.dal.dataobject.weibo.WeiboHotSearchDO;
import cn.iocoder.yudao.module.other.service.weibo.WeiboHotSearchService;

@Tag(name = "管理后台 - 微博热搜")
@RestController
@RequestMapping("/other/weibo-hot-search")
@Validated
public class WeiboHotSearchController {

    @Resource
    private WeiboHotSearchService weiboHotSearchService;

    @PostMapping("/create")
    @Operation(summary = "创建微博热搜")
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:create')")
    public CommonResult<Long> createWeiboHotSearch(@Valid @RequestBody WeiboHotSearchSaveReqVO createReqVO) {
        return success(weiboHotSearchService.createWeiboHotSearch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微博热搜")
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:update')")
    public CommonResult<Boolean> updateWeiboHotSearch(@Valid @RequestBody WeiboHotSearchSaveReqVO updateReqVO) {
        weiboHotSearchService.updateWeiboHotSearch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微博热搜")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:delete')")
    public CommonResult<Boolean> deleteWeiboHotSearch(@RequestParam("id") Long id) {
        weiboHotSearchService.deleteWeiboHotSearch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微博热搜")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:query')")
    public CommonResult<WeiboHotSearchRespVO> getWeiboHotSearch(@RequestParam("id") Long id) {
        WeiboHotSearchDO weiboHotSearch = weiboHotSearchService.getWeiboHotSearch(id);
        return success(BeanUtils.toBean(weiboHotSearch, WeiboHotSearchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微博热搜分页")
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:query')")
    public CommonResult<PageResult<WeiboHotSearchRespVO>> getWeiboHotSearchPage(@Valid WeiboHotSearchPageReqVO pageReqVO) {
        PageResult<WeiboHotSearchDO> pageResult = weiboHotSearchService.getWeiboHotSearchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeiboHotSearchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微博热搜 Excel")
    @PreAuthorize("@ss.hasPermission('other:weibo-hot-search:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeiboHotSearchExcel(@Valid WeiboHotSearchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeiboHotSearchDO> list = weiboHotSearchService.getWeiboHotSearchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微博热搜.xls", "数据", WeiboHotSearchRespVO.class,
                        BeanUtils.toBean(list, WeiboHotSearchRespVO.class));
    }

}